package com.sdgakj.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdgakj.entity.Notes;
import com.sdgakj.mvc.framework.dto.ValueDto;
import com.sdgakj.mvc.framework.util.ConditionUtil;
import com.sdgakj.service.notes.NotesService;
import com.sdgakj.sql.OptimizeSelectSqlImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by 鲁俊杰 on 2017/4/17.
 * 家族大事记的控制层接口
 */
@RestController
@RequestMapping(value = "/api/v1/notes")
public class NotesRestController {

    @Autowired
    private NotesService notesService;

    private static Logger logger = LoggerFactory.getLogger(MsgRestController.class);

    private static final String PAGE_SIZE = "10";


    /**
     * 列表页，
     * @param noteStr 前台传过来的Notes对象的json字符串，用户条件查询
     *                目前只做了按标题查询，后期可以添加其他查询方式
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    private JSONObject list(
            @RequestParam(value = "note" , defaultValue = "") String noteStr,
            @RequestParam(value = "fId" , defaultValue = "") String familyId,
            @RequestParam(value = "pageNo" , defaultValue = "1") int pageNo,
            @RequestParam(value = "pageSize" , defaultValue = PAGE_SIZE) int pageSize,
            @RequestParam(value = "sortType", defaultValue = "auto") String sortType
    ){
        logger.info("get notes list ...");
        JSONObject result = new JSONObject();
        Notes notes=null;
        try{
            if(!"".equals(noteStr))
                notes = JSONArray.parseObject(noteStr,Notes.class);
        }catch (Exception e){
            e.printStackTrace();
            result.put("resCode", "0");
            result.put("resMsg", "数据解析错误!");
            return result;
        }
        Map<String, Object> searchParams = new HashMap<>();
        Map<String, OptimizeSelectSqlImpl.ORDER> orderMap = new LinkedHashMap<>();
        orderMap.put("fnUpdatetime", OptimizeSelectSqlImpl.ORDER.DESC);
        if(notes!=null){
            if (notes.getFnTitle()!=null&&!"".equals(notes.getFnTitle())){
                searchParams.put(ConditionUtil.Operator.LIKE + "_fn_title", notes.getFnTitle());
            }
        }
        return notesService.getNoteByPage(searchParams, pageNo, pageSize, sortType);
    }

    /**
     * 添加大事记
     * @return
     */
    @RequestMapping(value="/create",method=RequestMethod.POST)
    public JSONObject createNotes(@RequestParam(value = "notes",defaultValue = "") String jsonData){
        logger.info("create notes ..."+jsonData);
        JSONObject result=new JSONObject();
        if(!"".equals(jsonData)){
            result.put("resCode", "0");
            result.put("resMsg", "请求数据异常!");
            return result;
        }
        Notes notes=null;
        try{
            notes=JSONArray.parseObject(jsonData,Notes.class);
        }catch (Exception e){
            result.put("resCode", "0");
            result.put("resMsg", "JSON数据解析异常!");
            return result;
        }
        return  notesService.saveNotes(notes);
    }

    /**
     * 修改大事件
     * @param jsonData
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public JSONObject updateNotes(@RequestParam(value = "notes",defaultValue = "") String jsonData){
        JSONObject result=new JSONObject();
        if(!"".equals(jsonData)){
            result.put("resCode", "0");
            result.put("resMsg", "请求数据异常!");
            return result;
        }
        Notes notes=null;
        Notes oldNotes=null;
        try{
            notes=JSONArray.parseObject(jsonData,Notes.class);
        }catch (Exception e){
            result.put("resCode", "0");
            result.put("resMsg", "JSON数据解析异常!");
            return result;
        }
        if (notes!=null&&notes.getId()!=0){
            oldNotes=notesService.getNoteById(notes.getId());
        }

        oldNotes.setFnTitle(notes.getFnTitle());
        oldNotes.setUpdatetime(new java.sql.Timestamp(new java.util.Date().getTime()));
        if(!"".equals(notes.getFnImages())){
            oldNotes.setFnImages(notes.getFnImages());
        }
        return notesService.updateNotes(oldNotes);
    }

    /**
     *  通过id删除大事记
     * @param notesId
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public JSONObject delete(@PathVariable(value = "id") Long notesId){
        JSONObject result=new JSONObject();
        if (notesId==null){
            result.put("resCode","0");
            result.put("resMsg","请求参数异常！");
            return result;
        }
        return notesService.deleteNotesById(notesId);
    }

    /**
     * 通过id获得大事记详情
     * @param notesId
     * @return
     */
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.GET)
    public JSONObject detail(@PathVariable(value = "id") Long notesId){
        JSONObject result=new JSONObject();
        if (notesId==null){
            result.put("resCode","0");
            result.put("resMsg","请求参数异常！");
            return result;
        }
        result.put("data",notesService.getNoteById(notesId));
        return result;
    }
}
